﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace MiddleTier
{
    public sealed class Facade
    {
        public List<Student> Students { get; private set; }
        public List<Teacher> Teachers { get; private set; }
        public List<Class> Classes { get; private set; }
        
        private Facade()
        {
            this.Students = new List<Student>();
            this.Teachers = new List<Teacher>();
            this.Classes = new List<Class>();

            // Þurfum núna að tengjast gagnagrunni í gegnum DAL til að ná í gögn ef einhver eru.            
            this.Students = (from s in DAL.GetDataContext.Students
                            select new Student(s)).ToList();

            this.Teachers = (from t in DAL.GetDataContext.Teachers
                             select new Teacher(t)).ToList();

            
            this.Classes = (from c in DAL.GetDataContext.Classes
                             select new Class(c)).ToList();
            
            // Setjum inn þau námskeið í kennara sem hver kennari kennir
            foreach (var t in this.Teachers)
            {
                foreach (var c in this.Classes)
                {
                    foreach(var t2 in c.Teachers)
                    {
                        if (t2.ID == t.ID)
                        {
                            t.Classes.Add(c);
                            break;
                        }
                    }
                }
            }

            // Gerum sambærilegt fyrir stúdenta
            foreach (var s in this.Students)
            {
                foreach (var c in this.Classes)
                {
                    foreach (var s2 in c.Students)
                    {
                        if (s2.ID == s.ID)
                        {
                            s.Classes.Add(c);
                            break;
                        }
                    }
                }
            }

        }


        public static Facade getInstance
        {
            get { return NestedFacede.facade; }
        }
        
        // Notum innri klasa til að tryggja þráða öryggi fyrir singleton.
        // Sjá http://www.yoda.arachsys.com/csharp/singleton.html
        internal class NestedFacede
        {
            static NestedFacede()
            {
                ; // Gerum ekkert hér
            }

            internal static readonly Facade facade = new Facade();
            
        }

    }

    /// <summary>
    /// Útvíkkum virkni klasa í DAL með erfðum!
    /// </summary>
    public class Student : DataAccessLayer.Student
    {
        public List<Class> Classes { get; private set; }
        public Student(string name, string email) : base(name, email)
        {
            this.Classes = new List<Class>();
        }

        // Public default constructor
        public Student()
        {
            this.Classes = new List<Class>();
        }

        public Student(DataAccessLayer.Student student)
        {
            this.Classes = new List<Class>();
            this.Name = student.Name;
            this.ID = student.ID;
            this.Email = student.Email;
        }

        /// <summary>
        /// Vistar þetta tilvik af stúdent.
        /// Ef ID er ekki til þá er búið til nýtt tilvik, annars er viðkomandi stúdent uppfærður.
        /// </summary>
        /// <returns>True ef aðferð tókst</returns>
        public bool Save()
        {
            DAL.GetDataContext.Students.Attach(this);
            DAL.GetDataContext.SubmitChanges();
            return true;
        }

        public void Learn()
        {
            
        }
    }

    public class Teacher : DataAccessLayer.Teacher
    {
        public List<Class> Classes { get; private set; }
        public Teacher(string name, string email) : base(name, email)
        {
            this.Classes = new List<Class>();
        }

        // Copy constructor frá grunn týpu
        public Teacher(DataAccessLayer.Teacher teacher)
        {
            this.Email = teacher.Email;
            this.Name = teacher.Name;
            this.ID = teacher.ID;
            this.Classes = new List<Class>();
        }

        public void Teach()
        {

        }

    }

    public class Class : DataAccessLayer.Class
    {
        public Class(String className)
        {
            this.ClassName = className;
        }

        public Class(DataAccessLayer.Class c)
        {
            this.ClassName = c.ClassName;
            this.ClassNumber = c.ClassNumber;            
        }

        public int NumberOfStudents
        {
            get
            {
                return this.Students.Count();
            }
        }

        public int NumberOfTeachers
        {
            get
            {
                return this.Teachers.Count();
            }
        }
    }

}
